package gw.com.android.ui.positions;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.gwtsz.gts2.hx.R;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import gw.com.android.app.AppContances;
import gw.com.android.app.AppMain;
import gw.com.android.model.ConfigType;
import gw.com.android.model.DataManager;
import gw.com.android.utils.ColorThemeUtil;
import gw.com.jni.library.terminal.GTSConst;
import www.com.library.model.DataItemDetail;
import www.com.library.util.CommonUtils;
import www.com.library.util.StringUtils;

/**
 * Created by reeta.zou on 2016/7/5.
 * 仓位界面用户资金栏自定义控件
 */
public class AccountInfoView extends RelativeLayout {
    private LayoutInflater inflater;
    private View layout;
    @BindView(R.id.three_layout)
    View mThreeLayout;
    @BindView(R.id.four_layout)
    View mFourLayout;
    /**用户名 */
    @BindView(R.id.loginname_value)
    TextView mAccountView;
    /**净值 */
    @BindView(R.id.equity_value)
    TextView mNetWorthView;
    /**可用资金 */
    @BindView(R.id.avail_margin_value)
    TextView mAvailableView;
    /**按金水平 */
    @BindView(R.id.margin_level_ratio_value)
    TextView mMarginLevelView;
    /**总净盈亏 */
    @BindView(R.id.profit_value)
    TextView mProfitView;
    /**货币类型*/
    @BindView(R.id.tv_currency_type)
    TextView mAccountTypeView;
    /**余额 */
    @BindView(R.id.balance_value)
    TextView mBalanceView;
    /**占用资金 */
    @BindView(R.id.lock_money_vlaue)
    TextView mOccupyView;
    /**强制平仓点 */
    @BindView(R.id.stop_loss_ratio_value)
    TextView mForceLevelView;
    /**下拉和收起箭头*/
    @BindView(R.id.arraw_up)
    ImageView mArrow;

    private DataItemDetail mAccountInfo;


    public AccountInfoView(Context context) {
        super(context);
        initView(context);
    }

    public AccountInfoView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    public AccountInfoView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        initView(context);
    }

    /**
     * 初始化资金控件
     * @author yuye.zou
     */
    private void initView(Context context) {
        if (!this.isInEditMode()) {
            inflater = LayoutInflater.from(context);
            layout = inflater.inflate(R.layout.position_account_info_view, this, true);
            ButterKnife.bind(this, layout);
            mArrow.setSelected(false);

            updateData();
        }
    }

    public void updateData(){
        mAccountInfo = DataManager.instance().mAccount;
        if (null == mAccountInfo) {
            mAccountView.setText(StringUtils.NA_MSG);
            mBalanceView.setText(StringUtils.NA_MSG);
            mAvailableView.setText(StringUtils.NA_MSG);
            mMarginLevelView.setText(StringUtils.NA_MSG);
            mProfitView.setText(StringUtils.NA_MSG);
            mOccupyView.setText(StringUtils.NA_MSG);
            mNetWorthView.setText(StringUtils.NA_MSG);
            mForceLevelView.setText(StringUtils.NA_MSG);
            return;
        }

        //账户(服务端传送)
        if(!mAccountInfo.isValidDetailData()){
            mBalanceView.setText(StringUtils.NA_MSG);
            mOccupyView.setText(StringUtils.NA_MSG);
            mForceLevelView.setText(StringUtils.NA_MSG);
            mAccountView.setText(StringUtils.NA_MSG);
            mAccountTypeView.setVisibility(View.GONE);
        }else{
            if ( null != mAccountInfo.getString(GTSConst.JSON_KEY_NAME)) {
//            Logger.i("account is " + mAccountInfo.account);
                mAccountView.setText(mAccountInfo.getString(GTSConst.JSON_KEY_NAME)+"");
                if (mAccountInfo.getString(GTSConst.JSON_KEY_TYPE).equals("USD")) {
                    mAccountTypeView.setText("("+ AppMain.getAppString(R.string.USD)+")");
                }else if(mAccountInfo.getString(GTSConst.JSON_KEY_TYPE).equals("CNY")){
                    mAccountTypeView.setText("("+AppMain.getAppString(R.string.CNH)+")");
                }else{
//                Logger.i("mAccountInfo.accoutType == " + mAccountInfo.accoutType);
                    mAccountTypeView.setText("");
                }
                mAccountTypeView.setVisibility(View.VISIBLE);
            }
            //余额(服务端传送)
            mBalanceView.setText(mAccountInfo.getString(GTSConst.JSON_KEY_BALANCE));
            //占用资金(UI端计算 所有持仓头寸所需的保证金之总和)
            mOccupyView.setText(mAccountInfo.getString(GTSConst.JSON_KEY_OCCUPY));
            //强制平仓点(服务端传送)
            mForceLevelView.setText(mAccountInfo.getString(GTSConst.JSON_KEY_FORCELEVEL));
        }

        //以下跟仓位有关的，需等仓位来了才显示
        //净值(UI端计算  净值=余额+总净盈亏)
        if (!mAccountInfo.isValidDetailData() || !DataManager.instance().getLoadDataState(ConfigType.TAB_POSITION_TYPE)) {
            mNetWorthView.setText(StringUtils.NA_MSG);
            mAvailableView.setText(StringUtils.NA_MSG);
            mMarginLevelView.setText(StringUtils.NA_MSG);
            mProfitView.setText(StringUtils.NA_MSG);
        } else {
            mNetWorthView.setText(mAccountInfo.getString(GTSConst.JSON_KEY_NETWORTH));
            //可用资金(UI端计算  净值-占用保证金)
            mAvailableView.setText(mAccountInfo.getString(GTSConst.JSON_KEY_AVAILABLE));
            //按金水平%(UI端计算 当前净值/占用保证金)
            mMarginLevelView.setText(mAccountInfo.getString(GTSConst.JSON_KEY_MARGINLEVEL));
            //总净盈亏(UI端计算 当前所有持仓头寸的净盈亏的总和)
            mProfitView.setText(mAccountInfo.getString(GTSConst.JSON_KEY_PROFIT));
            if(mAccountInfo.getInt(GTSConst.JSON_KEY_PROFITSTATE) == 0){
                mProfitView.setTextColor(ColorThemeUtil.instance().color_b);
            }else{
                ColorThemeUtil.instance().setPriceColor(mProfitView, mAccountInfo.getInt(GTSConst.JSON_KEY_PROFITSTATE));
            }
            if(mAccountInfo.getInt(GTSConst.JSON_KEY_NETWORTHSTATE) == AppContances.PRICE_DECREASE){
                mNetWorthView.setTextColor(ColorThemeUtil.instance().color_red);
            }else{
                mNetWorthView.setTextColor(ColorThemeUtil.instance().color_b);
            }
        }
    }

    @OnClick(R.id.user_info_view)
    void onFoldView(){
        if(CommonUtils.isFastDoubleClick()){
            return;
        }
        if (mThreeLayout.getVisibility() == View.VISIBLE) {
            mThreeLayout.setVisibility(View.GONE);
            mFourLayout.setVisibility(View.GONE);
            mArrow.setSelected(false);
            mArrow.setImageResource(R.mipmap.a_small_developdown);
        } else {
            mThreeLayout.setVisibility(View.VISIBLE);
            mFourLayout.setVisibility(View.VISIBLE);
            mArrow.setSelected(true);
            mArrow.setImageResource(R.mipmap.a_small_developup);
        }
    }
}
